from fastapi import APIRouter, Query, status
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi import Request

from app.controllers.accounts_controllers import (DeleteAccountController,
                                                  EditAccountController,
                                                  GetAccountByIdController,
                                                  SearchAccountsController)
from app.schemas.accounts_schemas import (EditAccountRequestSchema,
                                          GetAccountResponseSchema)

account_router = APIRouter(prefix="/accounts", tags=["Accounts routers"])
templates = Jinja2Templates(directory="templates")


@account_router.get("/search", status_code=status.HTTP_200_OK)
async def search_accounts(
        first_name: str = Query(None, alias="firstName"),
        last_name: str = Query(None, alias="lastName"),
        email: str = Query(None, alias="email"),
        from_param: int = Query(None, alias="from"),
        size: int = Query(None, alias="size"),
):
    controller = SearchAccountsController(
        first_name=first_name,
        last_name=last_name,
        email=email,
        from_param=from_param,
        size=size,
    )
    return await controller.execute()


@account_router.get("", response_class=HTMLResponse)
async def account_front(request: Request):
    return templates.TemplateResponse(
        request=request, name="account_manager.html", context={"id": 1}
    )


@account_router.get("/{accountId}", status_code=status.HTTP_200_OK)
async def get_account(accountId: int = None) -> GetAccountResponseSchema:
    controller = GetAccountByIdController(account_id=accountId)
    return await controller.execute()


@account_router.put("/{accountId}", status_code=status.HTTP_200_OK)
async def edit_account(
        request: EditAccountRequestSchema, accountId: int = None
) -> GetAccountResponseSchema:
    controller = EditAccountController(account_id=accountId, request=request)
    return await controller.execute()


@account_router.delete("/{accountId}", status_code=status.HTTP_200_OK)
async def delete_account(accountId: int = None) -> None:
    controller = DeleteAccountController(account_id=accountId)
    return await controller.execute()
